/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.luban.account.main.repository.impl;


import cn.tedu.luban.account.main.converter.AccountConverter;
import cn.tedu.luban.account.main.dao.AccountDAO;
import cn.tedu.luban.account.main.repository.AccountRepository;
import cn.tedu.luban.account.protocol.bo.AccountBO;
import cn.tedu.luban.account.protocol.dos.AccountDO;
import cn.tedu.luban.account.protocol.param.AccountCreateParam;
import cn.tedu.luban.account.protocol.param.AccountParam;
import cn.tedu.luban.account.protocol.param.AccountPayParam;
import cn.tedu.luban.account.protocol.query.AccountQuery;
import com.tedu.inn.protocol.enums.StatusRecord;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AccountRepositoryImpl implements AccountRepository {
    @Autowired
    private AccountConverter accountConverter;

    @Autowired(required = false)
    private AccountDAO accountDao;

    @Override public Long save(AccountCreateParam accountParam) {
        AccountDO account = this.accountConverter.param2do(accountParam);
        if (account.getId() != null) {
            this.accountDao.update(account);
            return account.getId();
        }
        this.accountDao.insert(account);
        return  account.getId();
    }
    @Override public Integer disable(Long id) {
        AccountParam accountParam = new AccountParam();
        accountParam.setId(id);
        accountParam.setStatus(StatusRecord.DISABLE.getStatus());
        AccountDO accountDO = accountConverter.param2do(accountParam);
        return this.accountDao.changeStatus(accountDO);
    }

    @Override public Integer enable(Long id) {
        AccountParam accountParam = new AccountParam();
        accountParam.setId(id);
        accountParam.setStatus(StatusRecord.ENABLE.getStatus());
        AccountDO accountDO = accountConverter.param2do(accountParam);
        return this.accountDao.changeStatus(accountDO);
    }

    @Override public Long updateSettlingAmount(AccountPayParam param) {
        AccountDO accountDO= accountConverter.param2do(param);
        return accountDao.updateSettlingAmount(accountDO);
    }

    @Override public Long updateTotalAmount(AccountPayParam param) {
        AccountDO accountDO= accountConverter.param2do(param);
        return accountDao.updateTotalAmount(accountDO);
    }

    @Override public void settleAccount(AccountParam param) {
        AccountDO accountDO= accountConverter.param2do(param);
        accountDao.updateSettlingAmountAndTotalAmount(accountDO);
    }

    @Override public AccountBO getAccountByUserId(Long userId) {
        AccountDO accountDO=accountDao.selectAccountByUserId(userId);
        return accountConverter.do2bo(accountDO);
    }

    @Override public AccountBO getAccount(Long id) {
        AccountDO account = this.accountDao.getEntity(id);
        return this.accountConverter.do2bo(account);
    }

    @Override public List<AccountBO> queryAccounts(
        AccountQuery accountQuery) {
        List<AccountDO> accountList = this.accountDao.queryAccounts(this.accountConverter.query2dbQuery(accountQuery));
        return this.accountConverter.dos2bos(accountList);
    }

    @Override public Long getAccountCount(AccountQuery accountQuery) {
        return this.accountDao.countAccount(this.accountConverter.query2dbQuery(accountQuery));
    }
}